<?php

require_once(WSB_MODULES_PATH . 'cms/Url.php');

class Cms_PageController extends Zend_Controller_Action
{

    public function indexAction()
    {
    	$this->_helper->Redirect(Cms_Url::containerEditUrl());
    }

    /*
    public function treeAction()
	{
    	$table = new Cms_Model_DbTable_Language();
    	$where = $table->getAdapter()->quoteInto('deleted=?', 0);
    	if(count($table->fetchAll($where)) == 0) {
			$this->render('noLanguages');
    	} else {
			$this->view->sitemapObj = new WSB_Sitemap();
			$this->render();
    	}
    	// Page Tree rendern
    	$this->render('page-tree', 'left', true);
    }
    */

    public function cutAction()
	{
    	$this->view->page_id = $this->_getParam('page_id');
		$this->view->sitemapObj = new WSB_Sitemap(WSB_Sitemap::MODE_PASTE);
		$this->view->sitemapObj->setIdPage($this->_getParam('page_id'));
		WSB_Log::info(sprintf('Cutted page (id=%s)', $this->_getParam('page_id')));

		$this->render();

    	// Page Tree rendern
    	$this->render('container-tree', 'left', true);
    }

    public function pasteAction()
	{
    	$table = new Cms_Model_DbTable_Page();
		$table->pastePage($this->_getParam('cutIdPage'), $this->_getParam('pid'), $this->_getParam('sort'));
		$this->_helper->FlashMessenger->addMessage('Seite wurde verschoben.');
		WSB_Log::info(sprintf('Paste page (id=%s)', $this->_getParam('cutIdPage')));
    	$this->_helper->Redirect(Cms_Url::containerEditUrl($this->_getParam('cutIdPage')));
    }

    /*
    public function hideAction()
	{
		$table = new Cms_Model_DbTable_Page();
		$where = $table->getAdapter()->quoteInto('id=?', $this->_getParam('page_id'));
    	$row = $table->fetchRow($where);
    	$row->hidden = 1;
    	$row->save();
		WSB_Log::info(sprintf('Hided row %s with id %s', $row->getTableName(), $this->_getParam('page_id')));
    	$this->_helper->Redirect(Cms_Url::pageOverviewUrl());
    }
    */

    /*
    public function unhideAction()
	{
		$table = new Cms_Model_DbTable_Page();
		$where = $table->getAdapter()->quoteInto('id=?', $this->_getParam('page_id'));
    	$row = $table->fetchRow($where);
    	$row->hidden = 0;
    	$row->save();
		WSB_Log::info(sprintf('Unhided row %s with id %s', $row->getTableName(), $this->_getParam('page_id')));
    	$this->_helper->Redirect(Cms_Url::pageOverviewUrl());
    }
    */

    /**
     * Edit action
     *
     * @todo Validierung der POST-Daten
     */
    public function editAction()
	{

    	$request = $this->getRequest();
    	$page_id = intval($this->_getParam('page_id'));

    	$table = new Cms_Model_DbTable_Language();
    	$where = $table->getAdapter()->quoteInto('deleted=?', 0);
    	$languages = $table->fetchAll($where, 'id ASC');
    	if(count($languages) == 0) {
    		$this->render('no-languages');
    		return;
    	}
    	$this->view->languages = $languages;

    	$table = new Cms_Model_DbTable_Page();
    	if(!empty($page_id)) {
    		$where = $table->getAdapter()->quoteInto('id=?', $page_id);
    		$page = $table->fetchRow($where);
    	} else {
    		$page = $table->createRow();
    	}

    	if($request->isPost()) {

    		// Ausgewaehlte Sprachen speichern
    		$post = $request->getPost('PageLanguage');
    		$idLanguages = isset($post['language_id']) ? $post['language_id'] : array() ;
			if(count($idLanguages) > 0) {
				$plTable = new Cms_Model_DbTable_PageLanguage();
				// Alle Eintraege loeschen
				$where = $plTable->getAdapter()->quoteInto('page_id=?', $page_id);
				$plTable->delete($where);
				foreach($idLanguages AS $language_id=>$checked) {
					if($checked == 1) {
						$pageLanguage = $plTable->createRow();
						$pageLanguage->page_id = $page_id;
						$pageLanguage->language_id = $language_id;
						$pageLanguage->save();
					}
				}
			}

    		// PageOverlay
    		$pageOverlays = $request->getPost('PageOverlay');
    		if(is_array($pageOverlays)) {
    			$poTable = new Cms_Model_DbTable_PageOverlay();
    			foreach($pageOverlays AS $language_id=>$pageOverlay) {
    				$where = array(
    					$poTable->getAdapter()->quoteInto('page_id=?', $page_id),
    					$poTable->getAdapter()->quoteInto('language_id=?', $language_id),
    					$poTable->getAdapter()->quoteInto('deleted=?', 0)
    				);
					$fetchedOverlays = $poTable->fetchAll($where);
					if(count($fetchedOverlays) > 0) {
						// Update
	    				$set = array(
	    					'title' => $pageOverlay['title']
	    				);
	    				$poTable->update($set, $where);
					} else {
						// Insert
						$data = array(
    						'page_id' => $page_id,
    						'language_id' => $language_id,
    						'title' => $pageOverlay['title']
    					);
						$poItem = $poTable->createRow($data);
						$poItem->save();
					}
    			}
    		}

    		$page->setFromArray($request->getPost('Page'));
    		$page->save();
    		$this->_helper->FlashMessenger->addMessage($this->_helper->Translate('page saved'));

			WSB_Log::info(sprintf('Saved page (id=%s)', $page->id));

			if($request->getPost(WSB_BTN_SAVE_CLOSE) != '') {
				$this->_helper->Redirect(Cms_Url::containerEditUrl($page_id));
			}
			// Immer einen Redirect ausfuehren
			$this->_helper->Redirect(Cms_Url::pageEditUrl($this->_getParam('page_id')));

    	}

    	try {

	    	// Eintraege aus PageLanguage auslesen
	    	$plTable = new Cms_Model_DbTable_PageLanguage();
	    	$where = $plTable->getAdapter()->quoteInto('page_id=?', $page_id);
	    	$this->view->pageLanguages = $plTable->fetchAll($where, 'language_id ASC');

	    	// Eintraege aus PageOverlay auslesen
	    	$poTable = new Cms_Model_DbTable_PageOverlay();
	    	$where = $poTable->getAdapter()->quoteInto('page_id=?', $page_id);
	    	$this->view->pageOverlays = $poTable->fetchAll($where, 'language_id ASC');

	    	$tree = WSB_Node_Page::getTree();
	    	$this->view->languageVariants = $tree->getLanguageVariants($page->id);

    		$this->view->overviewUrl = Cms_Url::containerEditUrl($page_id);
    		$this->view->page = $page;
    		$table = new Cms_Model_DbTable_Template();
    		$pairs = $table->fetchPairs('id', 'name', 'deleted=0');
    		$pairs[''] = '';
    		ksort($pairs);
    		$this->view->templateOptions = $pairs;
    		$this->render();

    	}
    	catch(Exception $e) {
    		$this->_response->appendBody($e->getMessage());
    	}

    	// Page Tree rendern
    	$this->render('container-tree', 'left', true);

    }

    public function addAction()
	{
    	$pid = $this->_getParam('pid');
    	$sort = $this->_getParam('sort');
    	$this->view->page_id = $this->_getParam('page_id');
    	if(is_numeric($pid) && is_numeric($sort)) {

    		$table = new Cms_Model_DbTable_Page();
    		$page_id = $table->addPage($pid, $sort);

    		$this->_helper->FlashMessenger->addMessage($this->_helper->Translate('page added'));
			WSB_Log::info(sprintf('Added page (id=%s)', $page_id));

    		$this->_helper->Redirect(Cms_Url::pageEditUrl($page_id));
    	} else {
			$this->view->sitemapObj = new WSB_Sitemap(WSB_Sitemap::MODE_ADD);
			$this->render();
    	}

    	// Page Tree rendern
    	$this->render('container-tree', 'left', true);
    }

    public function deleteAction()
	{
		$page_id = intval($this->_getParam('page_id', 0));
		$this->view->page_id = $page_id;
		if($page_id > 0) {
			$branch = WSB_Node_Page::getBranch($page_id);
			if($branch->hasChildren()) {
				$this->view->error = 'Seiten mit Unterseiten k&ouml;nnen nicht gel&ouml;scht werden.';
				$this->render();
    			$this->render('container-tree', 'left', true);
			} else {
				$table = new Cms_Model_DbTable_Page();
				$table->deletePage($page_id, $this->_getParam('pid'));
				$this->_helper->FlashMessenger->addMessage($this->_helper->Translate('delete: successfully'));
				WSB_Log::info(sprintf('Deleted page (id=%s)', $page_id));
				$this->_helper->Redirect(Cms_Url::containerEditUrl());
			}
		}
    }

}
